package com.meihua.shellapp.activity.personalcenter;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.aspsine.irecyclerview.IRecyclerView;
import com.lzy.okgo.OkGo;
import com.meihua.shellapp.R;
import com.meihua.shellapp.adapter.personalcenter.MyReleaseAdapter;
import com.meihua.shellapp.base.BaseActivity;
import com.meihua.shellapp.base.MyApplication;
import com.meihua.shellapp.bean.MyReleaseBean;
import com.meihua.shellapp.databean.ReleaseDataBean;
import com.meihua.shellapp.utils.MyStringCallBack;
import com.meihua.shellapp.utils.ToastUtils;
import com.meihua.shellapp.utils.URL;
import com.meihua.shellapp.view.footer.LoadMoreFooterView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * @auther wangjunguang
 * @function 我的发布界面
 * <p>
 * Created by admin on 2017/9/29.
 */
public class MyReleaseActivity extends BaseActivity {
    @Bind(R.id.iv_top_left)
    ImageView iv_top_left;
    @Bind(R.id.tv_title)
    TextView tv_title;
    @Bind(R.id.rv_content)
    IRecyclerView rv_content;
    @Bind(R.id.tv_top_right)
    TextView tv_top_right;

    private LoadMoreFooterView loadMoreFooterView;
    private MyReleaseAdapter mAdapter;
    private List<ReleaseDataBean> mData = new ArrayList<>();
    private int mPage = 1;

    @Override
    public int getLayoutId() {
        return R.layout.activity_myrelease;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        iv_top_left.setVisibility(View.VISIBLE);
        iv_top_left.setOnClickListener(this);
        tv_title.setVisibility(View.VISIBLE);
        tv_title.setText(getResources().getString(R.string.activity_my_release_title));
        tv_top_right.setVisibility(View.VISIBLE);
        tv_top_right.setText(getResources().getString(R.string.activity_my_release_submit));
        tv_top_right.setOnClickListener(this);
        // 初始化列表
        initList();
    }

    @Override
    public void createLoadData() {
        super.createLoadData();

    }

    @Override
    protected void onResume() {
        super.onResume();
        getData();
    }

    private void getData() {
        OkGo.<String>post(URL.getUrl().GETRELEASE)
                .params("mid", MyApplication.getInstance().getMId())
                .params("page", mPage)
                .execute(new MyStringCallBack<MyReleaseBean>(rv_content) {

                    @Override
                    public void onSuccessData(MyReleaseBean bean) {
                        super.onSuccessData(bean);
                        if (bean.getData() != null && bean.getData().size() > 0) {
                            if (mPage == 1) {
                                mData.clear();
                            }
                            mData.addAll(bean.getData());
                            mAdapter.setmList(mData);
                        } else {
                            if (mPage == 1) {
                                mData.clear();
                                mAdapter.setmList(mData);
                            }
                            endRefreshing(rv_content);
                            ToastUtils.getInstance().showToast(bean.getMsg());
                        }
                        mAdapter.notifyDataSetChanged();
                    }

                });
    }

    /**
     * 初始化发布列表
     */
    private void initList() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        rv_content.setLayoutManager(layoutManager);
        loadMoreFooterView = (LoadMoreFooterView) rv_content.getLoadMoreFooterView();
        mAdapter = new MyReleaseAdapter(this, mData);
        rv_content.setOnLoadMoreListener(this);
        rv_content.setOnRefreshListener(this);
        rv_content.setIAdapter(mAdapter);
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.tv_top_right://我要发布
                startActivity(new Intent(this, ReleaseActivity.class));
                break;
        }
    }

    @Override
    public void onLoadMore() {
        if (loadMoreFooterView.canLoadMore() && mAdapter.getItemCount() > 0) {
            loadMoreFooterView.setStatus(LoadMoreFooterView.Status.LOADING);
            mPage++;
            getData();
        }
    }

    @Override
    public void onRefresh() {
        mPage = 1;
        getData();
    }
}
